package cn.com.ethos.search;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Calendar;
import java.util.Properties;
import java.util.TimeZone;

import org.apache.log4j.Logger;

public class ParametersHelper {
	
	/**
	 * Save and load parameters file path    
	 */
	public static final String PARAM_FILE_PATH = "config/parameters.ini";
	
	private static Properties paramHashMap ;
	private static File file;
	
	static {
		initProperties();
		buildSystemParameters();
	}
	
	public static void initProperties() {
		file         = new File(PARAM_FILE_PATH);
		paramHashMap = new Properties();
		
		if(file.exists()) {
			try {
				paramHashMap.load(new FileInputStream(file));
			} catch(Exception e) {
				Logger.getLogger(ParametersHelper.class).warn("Load paramter file failed," + PARAM_FILE_PATH);
			}
		}
	}
	
	public static void buildSystemParameters() {
		paramHashMap.put("TODAY_STAMP",getTodayTimeStamp());
	}
	
	public static Properties getParamHashMap() {
		return paramHashMap;
	}

	public static void setParamHashMap(Properties paramHashMap) {
		ParametersHelper.paramHashMap = paramHashMap;
	}
	
	public static void setParam(String key,String value) {
		paramHashMap.put(key, value);
	}
	
	public static void save() 
				throws FileNotFoundException,IOException{
		paramHashMap.store(new FileOutputStream(file), "update file at " + new java.util.Date().toString());
	}
	
	public static String getTodayTimeStamp() {
		Calendar cal = Calendar.getInstance(TimeZone.getDefault());
		cal.set(cal.get(Calendar.YEAR),cal.get(Calendar.MONTH),cal.get(Calendar.DAY_OF_MONTH),0,0,0);
		return String.valueOf(cal.getTimeInMillis() / 1000);
	}
	
}
